package com.stormrage.edu.serv.learn.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.learn.LeaBunchApplyDao;
import com.stormrage.edu.dao.learn.LeaBunchDao;
import com.stormrage.edu.serv.acct.EntAcctServ;
import com.stormrage.edu.serv.learn.LeaApplyServ;
import com.stormrage.edu.serv.learn.LeaBunchOperServ;
import com.stormrage.edu.serv.learn.LeaBunchServ;
import com.stormrage.edu.serv.learn.LeaBunchUsuaServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.entity.ent.EntAcct;
import com.stormrage.entity.learn.LeaBunch;
import com.stormrage.entity.learn.LeaBunchApply;
import com.stormrage.entity.learn.LeaBunchOper;
import com.stormrage.entity.learn.LeaBunchUsua;
import com.stormrage.entity.learn.LeaTerm;
import com.stormrage.entity.learn.req.LeaBunchReq;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.DateUtil;
import com.stormragetech.core.util.FileUtil;
import com.stormragetech.core.util.NumberUtil;

/**
 * @Title: LeaApplyServImpl.java
 * @Package com.stormrage.edu.serv.learn.impl
 * @Description: 报名 Service
 * @author www.stormragetech.com
 * @date 2016年4月13日
 * @version V1.0
 */
@Service
public class LeaApplyServImpl implements LeaApplyServ {

	private static final Logger logger = LoggerFactory.getLogger(LeaApplyServImpl.class);

	@Autowired
	private LeaBunchDao leaBunchDao;

	@Autowired
	private LeaBunchApplyDao leaBunchApplyDao;

	@Autowired
	private EntAcctServ entAcctServ;

	@Autowired
	private LeaBunchServ leaBunchServ;

	@Autowired
	private LeaBunchOperServ leaBunchOperServ;

	@Autowired
	private LeaBunchUsuaServ leaBunchUsuaServ;

	@Autowired
	private LeaTermServ leaTermServ;

	@Override
	public Integer apply(LeaBunchReq arg0) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Apply 】- 【 STARTED 】");

		// ------------------ 获取公司ID ------------------
		EntAcct acct = entAcctServ.queryById(arg0.getAcctId());
		LeaBunchReq req = LeaBunchReq.instance(arg0.getTermId(), acct.getEntId());

		// ------------------ 获取班级ID ------------------
		Integer count = leaBunchServ.queryCountByTermEnt(req);
		logger.debug(" 报名数量为 >>>>>>>>" + count);
		Integer index;
		if (count.equals(0)) {
			// ------------------ 班级不存在，创建 ------------------
			LeaTerm term = leaTermServ.queryById(arg0.getTermId());

			LeaBunch bunch = LeaBunch.instance(arg0.getTermId());
			index = leaBunchDao.create(bunch);

			String name = "bunch" + DateUtil.dateToStr(new Date(), "yyyy") + NumberUtil.initZero(term.getMajorId(), 4)
					+ NumberUtil.initZero(acct.getEntId(), 4) + NumberUtil.initZero(index, 5);
			bunch = LeaBunch.instance(bunch, index, name);

			leaBunchDao.updateBunch(bunch);

			// ------------------ 添加报名信息 ------------------
			LeaBunchOper bunchOper = LeaBunchOper.instance(index);
			LeaBunchUsua bunchUsua = LeaBunchUsua.instance(index);
			leaBunchOperServ.create(bunchOper);
			leaBunchUsuaServ.create(bunchUsua);

		} else {
			// ------------------ 班级已存在，获取 ------------------
			List<LeaBunch> bunch = leaBunchServ.queryByTermEnt(req);
			index = bunch.get(0).getId();
		}

		// ------------------ 添加报名信息 ------------------
		LeaBunchApply apply = LeaBunchApply.instance(index, acct.getEntId(), arg0.getExcel());
		leaBunchApplyDao.create(apply);

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Apply 】- 【 COMPLETED 】");
		return index;

	}

	@Override
	public void add(LeaBunchReq arg0) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Apply 】- 【 STARTED 】");

		// ------------------ 获取公司ID ------------------
		EntAcct acct = entAcctServ.queryById(arg0.getAcctId());

		// ------------------ 验证班级是否可以添加 ------------------
		LeaBunchApply apply = queryByEntBunchLock(arg0.getTermId(), acct.getEntId(), 0);
		if (null != apply) {
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "已添加报名excel，请耐心等待审核",
					StormrageTechException.ERR_APP, "【 EntEnterpriseServ 】-【 Create 】");
		}

		// ------------------ 添加报名信息 ------------------
		apply = LeaBunchApply.instance(arg0.getTermId(), acct.getEntId(), arg0.getExcel());
		leaBunchApplyDao.create(apply);

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Apply 】- 【 COMPLETED 】");

	}

	@Override
	public String upload(final MultipartFile arg0, final String arg1) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Upload 】- 【 STARTED 】");

		// ------------------ 保存 Excel 文件 ------------------
		String fileName = FileUtil.saveFile(arg0, arg1, "excel");

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Upload 】- 【 COMPLETED 】");

		return fileName;

	}

	@Override
	public LeaBunchApply queryByEntBunchLock(final Integer bunchId, final Integer entId, final Integer locked)
			throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 queryByEntBunchLock 】- 【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bunchId", bunchId);
		map.put("entId", entId);
		map.put("locked", locked);

		LeaBunchApply entity = leaBunchApplyDao.queryByEntBunchLock(map);

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 queryByEntBunchLock 】- 【 COMPLETED 】");
		return entity;

	}

}
